package com.jjmmbb.baoSudoku;

import java.util.Calendar;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

import com.adb.util.FlashlightManager;
import com.adb.util.Util;

public class MainActivity extends BaseActivity implements OnClickListener {
	Button bt_easy, bt_general, bt_hard;
	// ImageView image;

	int countfinish;
	String version;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		int hours = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hours > 19 || hours < 7) {
			Comm.isNight = true;
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		bt_easy = (Button) findViewById(R.id.main_bt_easy); // 初级
		bt_general = (Button) findViewById(R.id.main_bt_general); // 专家
		bt_hard = (Button) findViewById(R.id.main_bt_hard); // 大师
		// image = (ImageView) findViewById(R.id.main_image);

		bt_easy.setOnClickListener(this);
		bt_general.setOnClickListener(this);
		bt_hard.setOnClickListener(this);

		ini();
		setNightMode();
	}

	public void ini() {
		// 读取屏幕分辨率容器
		DisplayMetrics dMetrics = new DisplayMetrics();
		((WindowManager) getWindowManager()).getDefaultDisplay().getMetrics(
				dMetrics);
		Comm.screenWidth = dMetrics.widthPixels;
		Comm.screenHeight = dMetrics.heightPixels;

		// 读取配置文件
		Util.getConfigFromPreferences(this);

		if (Comm.isFirst) {
			// gotoActivity(FirstActivity.class);
		}

		// 版本转换
		if (Comm.isPure) {
		}

		// 初始化main界面
		// image.setImageBitmap(getBitmpDrawableScaledByX(R.drawable.sys_bg));
	}

	@Override
	public void onClick(View v) {
		// playButton();
		Intent intent = new Intent();
		intent.setClass(this, StageListActivity.class);
		Bundle bundle = new Bundle();
		switch (v.getId()) {
		case R.id.main_bt_easy:
			bundle.putInt("stage", 1);
			intent.putExtras(bundle); // 传递数据过去
			startActivityForResult(intent, 0);
			break;
		case R.id.main_bt_general:
			bundle.putInt("stage", 2);
			intent.putExtras(bundle); // 传递数据过去
			startActivityForResult(intent, 0);
			break;
		case R.id.main_bt_hard:
			bundle.putInt("stage", 3);
			intent.putExtras(bundle); // 传递数据过去
			startActivityForResult(intent, 0);
			break;

		}
	}

	@Override
	protected void onPause() {
		FlashlightManager.disableFlashlight();
		super.onPause();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			FlashlightManager.disableFlashlight();
			finish();
			break;
		}

		return super.onKeyDown(keyCode, event);
	}

	// ------创建OptionsMenu--------------/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 0, 0, "统计");
		menu.add(0, 1, 0, "帮助");
		menu.add(0, 2, 0, "夜间模式");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			gotoActivity(AnalyseActivity.class);
			break;
		case 1:
			gotoActivity(HelpActivity.class);
			break;
		case 2:
			Comm.isNight = !Comm.isNight;
			setNightMode();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}